#include "dialog.h"
#include "ui_dialog.h"
#include <roundhandler.h>
#include <QGraphicsScene>
#include "mathcalc.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    initScene();

    connect(_handler, SIGNAL(setCoordinates(QPointF)),
            this, SLOT(newCoordinates(QPointF)));

    connect(ui->verticalSlider, SIGNAL(valueChanged(int)),
            this, SLOT(newZ(int)));
}

void Dialog::initScene()
{
    _scene = new QGraphicsScene();
    _scene->setSceneRect( -100.0, -100.0, 100.0, 100.0 );

    _handler = new RoundHandler();

    _scene->addItem(_handler);

    ui->graphicsView->setScene(_scene);
}

void Dialog::newZ(int z)
{
    _z = z;

    ui->label_6->setText(QString::number(_z));

    calc();
}


void Dialog::newCoordinates(QPointF newP)
{
    _x = newP.x();
    _y = newP.y();

    ui->label_2->setText(QString::number(_x));
    ui->label_4->setText(QString::number(_y));

    calc();
}

void Dialog::calc()
{

    int h1, h2, h3 = 0;

    if (!MathCalc::calculate(_x, _y, _z, h1, h2, h3))
    {
        ui->label_12->setStyleSheet("color: rgb(255, 0, 0);");
        ui->label_8->setStyleSheet("color: rgb(255, 0, 0);");
        ui->label_10->setStyleSheet("color: rgb(255, 0, 0);");
    }
    else
    {
        ui->label_12->setStyleSheet("");
        ui->label_8->setStyleSheet("");
        ui->label_10->setStyleSheet("");
    }

    ui->label_12->setText(QString::number(h1));
    ui->label_8->setText(QString::number(h2));
    ui->label_10->setText(QString::number(h3));
}


Dialog::~Dialog()
{
    delete _handler;
    delete _scene;
    delete ui;
}
